package com.ezjie.ielts.view.agenda;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout.LayoutParams;

import com.ezjie.ielts.R;
import com.ezjie.ielts.model.SixDate;

public class DateWidgetDayCell extends View {
	// 字体大小
	private static final int fTextSize = 28;

	// 基本元素
	private OnItemClick itemClick = null;
	private Paint pt = new Paint();
	private RectF rect = new RectF();
	private String sDate = "";

	// 当前日期
	private int iDateYear = 0;
	private int iDateMonth = 0;
	private int iDateDay = 0;
	private String iDateAll = "";

	// 布尔变量
	private boolean bSelected = false;
	private boolean bIsActiveMonth = false;
	private boolean bToday = false;
	private boolean bTouchedDown = false;
	private boolean bHoliday = false;
	private boolean hasRecord = false;
	private boolean bYesterday = false;

	public static int ANIM_ALPHA_DURATION = 100;

	private List<SixDate> sixdates;

	public interface OnItemClick {
		public void OnClick(DateWidgetDayCell item);
	}

	// 构造函数
	public DateWidgetDayCell(Context context, int iWidth, int iHeight) {
		super(context);
		setFocusable(true);
		setLayoutParams(new LayoutParams(iWidth, iHeight));
	}

	// 取变量值
	public Calendar getDate() {
		Calendar calDate = Calendar.getInstance();
		calDate.clear();
		calDate.set(Calendar.YEAR, iDateYear);
		calDate.set(Calendar.MONTH, iDateMonth);
		calDate.set(Calendar.DAY_OF_MONTH, iDateDay);
		return calDate;
	}

	// 设置变量值
	public void setData(int iYear, int iMonth, int iDay, Boolean bToday, Boolean bHoliday, int iActiveMonth,
			boolean hasRecord, boolean bYesterday, List<SixDate> sixdates) {
		iDateYear = iYear;
		iDateMonth = iMonth;
		iDateDay = iDay;

		this.sDate = Integer.toString(iDateDay);
		this.bIsActiveMonth = (iDateMonth == iActiveMonth);
		this.bToday = bToday;
		this.bHoliday = bHoliday;
		this.hasRecord = hasRecord;
		this.bYesterday = bYesterday;
		this.sixdates = sixdates;
		invalidate();
	}

	public void clearData() {
		sDate = "";
		itemClick = null;
		invalidate();
	}

	// 重载绘制方法
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		rect.set(0, 0, this.getWidth(), this.getHeight());
		rect.inset(0, 0);

		final boolean bFocused = IsViewFocused();

		drawDayView(canvas, bFocused);
		drawDayNumber(canvas);
	}

	public void drawArcView(Canvas canvas, int[] intss) {
		int total = 0;
		int tip = 0;
		for (int i : intss) {
			if (i != 0) {
				total += 1;
			}
		}
		for (int j = 0; j < intss.length; j++) {
			RectF rectF2 = new RectF(10, (getHeight() - getWidth()) / 2 + 10, getWidth() - 10,
					(getHeight() + getWidth()) / 2 - 10);
			Paint paint = new Paint();
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(4);
			paint.setAntiAlias(true);
			switch (j) {
			case 0:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_dgreen));
				}
				break;
			case 1:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_lgreen));
				}
				break;
			case 2:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_blue));
				}
				break;
			case 3:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_purple));
				}
				break;
			case 4:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_red));
				}
				break;
			case 5:
				if (intss[j] != 0) {
					paint.setColor(getResources().getColor(R.color.six_yellow));
				}
				break;
			default:
				break;

			}
			int arc = 360 * intss[j] / total;
			canvas.drawArc(rectF2, tip, arc, false, paint);
			tip += arc;
		}
	}

	public boolean IsViewFocused() {
		return (this.isFocused() || bTouchedDown);
	}

	// 绘制日历方格
	private void drawDayView(Canvas canvas, boolean bFocused) {

		if (bToday) {
			pt.setColor(getResources().getColor(R.color.six_yellow));
			pt.setAntiAlias(true);
			canvas.drawCircle(getWidth() / 2, getHeight() - 16, 7, pt);
		}

		if (sixdates != null) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			iDateAll = formatter.format(getDate().getTime());
			for (int i = 0; i < sixdates.size(); i++) {
				String date = sixdates.get(i).getDate();
				if (iDateAll.equals(date)) {
					drawArcView(canvas, sixdates.get(i).getInts());
					break;
				}
			}
		}

	}

	// 绘制日历中的数字
	public void drawDayNumber(Canvas canvas) {
		// draw day number
		pt.setTypeface(null);
		pt.setAntiAlias(true);
		pt.setShader(null);
		// pt.setFakeBoldText(true);
		pt.setTextSize(fTextSize);
		pt.setColor(getResources().getColor(R.color.text_black));
		pt.setUnderlineText(false);

		if (!bIsActiveMonth)
			pt.setColor(getResources().getColor(R.color.text_gray));

		if (bYesterday) {
			pt.setColor(getResources().getColor(R.color.text_gray));
			itemClick = null;
		}

		if (bSelected) {
			pt.setTextSize(fTextSize + 4);
			pt.setColor(getResources().getColor(R.color.six_yellow));
			pt.setTypeface(Typeface.DEFAULT_BOLD);
		}

		final int iPosX = (int) rect.left + ((int) rect.width() >> 1) - ((int) pt.measureText(sDate) >> 1);

		final int iPosY = (int) (this.getHeight() - (this.getHeight() - getTextHeight()) / 2 - pt.getFontMetrics().bottom);

		canvas.drawText(sDate, iPosX, iPosY, pt);

		pt.setUnderlineText(false);
	}

	// 得到字体高度
	private int getTextHeight() {
		return (int) (-pt.ascent() + pt.descent());
	}

	// 设置是否被选中
	@Override
	public void setSelected(boolean bEnable) {
		if (this.bSelected != bEnable) {
			this.bSelected = bEnable;
			this.invalidate();
		}
	}

	public void setItemClick(OnItemClick itemClick) {
		this.itemClick = itemClick;
	}

	public void doItemClick() {
		if (itemClick != null)
			itemClick.OnClick(this);
	}

	// 点击事件
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean bHandled = false;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			bHandled = true;
			bTouchedDown = true;
			invalidate();
			startAlphaAnimIn(DateWidgetDayCell.this);
		}
		if (event.getAction() == MotionEvent.ACTION_CANCEL) {
			bHandled = true;
			bTouchedDown = false;
			invalidate();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			bHandled = true;
			bTouchedDown = false;
			invalidate();
			doItemClick();
		}
		return bHandled;
	}

	// 点击事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean bResult = super.onKeyDown(keyCode, event);
		if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) {
			doItemClick();
		}
		return bResult;
	}

	// 不透明度渐变
	public static void startAlphaAnimIn(View view) {
		AlphaAnimation anim = new AlphaAnimation(0.5F, 1);
		anim.setDuration(ANIM_ALPHA_DURATION);
		anim.startNow();
		view.startAnimation(anim);
	}
}